package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLocationService;
import com.itheima.service.mongo.VisitorService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.NearUserVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.RecommendUserVo;
import com.itheima.vo.VisitorVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MakeFriendManager {

    @DubboReference
    private RecommendUserService recommendUserService;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private VisitorService visitorService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private UserLocationService userLocationService;

    //今日佳人
    public ResponseEntity findTodayBest() {
        //当前用户id ，也是要推荐给人的id
        Long toUserId = UserHolder.get().getId();

        RecommendUser recommendUser = recommendUserService.findTodayBest(toUserId);

        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setScore(RandomUtil.randomDouble(90, 99));
            recommendUser.setUserId(RandomUtil.randomLong(1, 20));

        }
        UserInfo userInfo = userInfoService.findById(recommendUser.getUserId());
        RecommendUserVo vo = new RecommendUserVo();
        vo.setUserInfo(userInfo);
        vo.setFateValue(recommendUser.getScore().longValue());
        return ResponseEntity.ok(vo);
    }
    // 推荐用户分页
    public ResponseEntity findRecommendUserVoByPage(Integer pageNum, Integer pageSize) {
        Long toUserId = UserHolder.get().getId();

        PageBeanVo pageBeanVo = recommendUserService.findRecommendUserByPage(toUserId, pageNum, pageSize);
        List<RecommendUser> recommendUses = (List<RecommendUser>) pageBeanVo.getItems();
        List<RecommendUserVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendUses)) {
            for (RecommendUser recommendUs : recommendUses) {
                UserInfo userInfo = userInfoService.findById(recommendUs.getUserId());
                RecommendUserVo vo = new RecommendUserVo();
                vo.setUserInfo(userInfo);
                vo.setFateValue(recommendUs.getScore().longValue());
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
    // 查询用户封面信息
    public ResponseEntity personalInfo(Long jiarenId) {

        Long toUserId = UserHolder.get().getId();
        RecommendUser recommendUser;
        if (jiarenId == toUserId) {
            recommendUser = new RecommendUser();
            recommendUser.setScore(99d);
            recommendUser.setUserId(jiarenId);

        }else{
            recommendUser = recommendUserService.personalInfo(jiarenId, toUserId);
            if (recommendUser==null) {
                recommendUser = new RecommendUser();
                recommendUser.setScore(RandomUtil.randomDouble(80,99));
            }
        }

        RecommendUserVo vo = new RecommendUserVo();

        UserInfo userinfo = userInfoService.findById(jiarenId);

        vo.setUserInfo(userinfo);
        vo.setFateValue(recommendUser.getScore().longValue());

        return ResponseEntity.ok(vo);
    }
    //是否喜欢//todo 是否喜欢
    public ResponseEntity alreadyLove(Long likeUserId) {
        Long userId = UserHolder.get().getId();

        Boolean aBoolean = recommendUserService.alreadyLove(likeUserId, userId);
        return ResponseEntity.ok(aBoolean);
    }
    // 最近访客
    public ResponseEntity visitors() {
        Long userId = UserHolder.get().getId();

        String lastAccessTime = stringRedisTemplate.opsForValue().get(ConstantUtil.LAST_ACCESS_TIME + userId);

        List<Visitor> visitorList;
        if (StrUtil.isNotEmpty(lastAccessTime)) {
            visitorList = visitorService.findVistiors(userId, Long.valueOf(lastAccessTime));
        } else {
            visitorList = visitorService.findVistiors(userId, System.currentTimeMillis());
        }

        List<VisitorVo> voList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(visitorList)) {
            for (Visitor visitor : visitorList) {
                VisitorVo vo = new VisitorVo();
                UserInfo userInfo = userInfoService.findById(visitor.getVisitorUserId());
                vo.setUserInfo(userInfo);
                vo.setFateValue(visitor.getScore().longValue());
                voList.add(vo);
            }
        }
        stringRedisTemplate.opsForValue().set(ConstantUtil.LAST_ACCESS_TIME + userId,
                String.valueOf(System.currentTimeMillis()));

        return ResponseEntity.ok(voList);



    }
    //上报地理位置
    public void location(Double longitude, Double latitude, String addrStr) {
        Long userId = UserHolder.get().getId();

        userLocationService.saveLocation(longitude, latitude, addrStr, userId);

    }
    // 搜附近
    public ResponseEntity searchNearUserVo(String gender, Long distance) {
        Long userId = UserHolder.get().getId();
        List<Long> nearUserIdList = userLocationService.searchNearUserId(userId, distance);
        List<NearUserVo> voList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(nearUserIdList)) {

            for (Long nearUserId : nearUserIdList) {
                if (nearUserId == userId) {
                    continue;
                }
                UserInfo userInfo = userInfoService.findById(nearUserId);
                if (!StrUtil.equals(gender, userInfo.getGender())) {
                    continue;
                }
                NearUserVo vo = new NearUserVo();
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setUserId(userInfo.getId());
                voList.add(vo);
            }
        }
        return ResponseEntity.ok(voList);
    }

    @DubboReference
    private QuestionService questionService;
    //查看陌生人问题
    public ResponseEntity strangerQuestions(Long jiarenId) {
        Question question = questionService.findByUserId(jiarenId);
        if (question == null) {
            question = new Question();
        }
        return ResponseEntity.ok(question.getStrangerQuestion());

    }
}
